package com.example.testapplication;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

/**
 * 列表 fragment
 **/
public class ListFragment extends Fragment {
    public static final String BUNDLE_TITLE = "bundle_title";
    private String title ="title";

    // 接收传参
   public static ListFragment newInstance(String title){
       ListFragment fragment = new ListFragment();
       Bundle bundle = new Bundle();
       bundle.putString(BUNDLE_TITLE,title);  // 提取常量 ctrl + alt +c
       fragment.setArguments(bundle);
       return fragment;
   }

    // 生命周期
    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(getArguments() != null){
            title = getArguments().getString(BUNDLE_TITLE); // 提取变量  ctrl + alt +f

        }
    }

    //  创建视图
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        // 新视图
        View view = inflater.inflate(R.layout.fragment_list, container, false);
        TextView textView = view.findViewById(R.id.textView2);
        textView.setText(title);

        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mOnTitleClickListener != null){
                    mOnTitleClickListener.onClick(title);
                }
            }
        });

        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    // fragment 向 activity传参
    // 设置set方法
    public void setmOnTitleClickListener(onTitleClickLister mOnTitleClickListener) {
        this.mOnTitleClickListener = mOnTitleClickListener;
    }
    // 定义变量
    onTitleClickLister mOnTitleClickListener;
   // 定义接口
   public interface onTitleClickLister{
       void onClick(String title);
   }

}
